from celery_task import app
from home.models import Banner
from home.serializer import BannerSerializer
from django.conf import settings
from django.core.cache import cache


@app.task
def update_banner():
    # 更新轮播图缓存任务
    # 只要task被执行一次，缓存中的轮播图就是最新的
    queryset = Banner.objects.all().filter(is_show=True,
                                           is_delete=False).order_by('-orders')[0:settings.BANNER_SIZE]
    ser = BannerSerializer(instance=queryset, many=True)
    # 处理路径
    for item in ser.data:
        item['image'] = settings.REMOTE_ADDR + item['image']
    cache.set('banner_cache_list', ser.data)
    return True
